let resizeTimer = null;
sessionStorage.viewWidth = document.body.clientWidth;
sessionStorage.docHeight = document.body.scrollHeight;

const winPage = {
    init: function () {
        this.isFirstOpen();
        this.winResize();
        this.resize();
        this.marquee();        
    },

    // 整体文字大小动态设置
    winResize: function () {
        let ele = document.getElementsByTagName("html")[0];
        let maxWidth = document.body.clientWidth;
        if (maxWidth >= 550) { maxWidth = 550 };
        size = maxWidth / 320 * 20;        
        ele.style.fontSize = size + "px";
    },

    //窗口调整时刷新页面
    resize: function () {
        window.onresize = () => {

            function resizeFn() {
                if (resizeTimer) { clearTimeout(resizeTimer) }
                resizeTimer = setTimeout(() => {
                    location.reload();
                }, 300);
            }

            if (Number(sessionStorage.viewWidth) == Number(document.body.clientWidth)) { return }
            else if (Number(sessionStorage.viewWidth) > 960 && Number(document.body.clientWidth) > 960) { return; }
            else if (Number(sessionStorage.viewWidth) > 960 && Number(document.body.clientWidth) < 960) { resizeFn() }
            else if (Number(sessionStorage.viewWidth) > 550 && Number(document.body.clientWidth) > 960) { resizeFn() }
            else if (Number(sessionStorage.viewWidth) > 550 && Number(document.body.clientWidth) > 550) { return }
            else { resizeFn() }
        }
    },


    //第一次打开页面，加载GGAD时，需要刷新一下页面
    isFirstOpen: function () {
        if (typeof (Storage) !== "undefined") {

            // 本地存储可用时
            if (sessionStorage.firstOpen !== "yes") {
                // 第一次打开页面时        
                sessionStorage.firstOpen = "yes";
                setTimeout(()=> { location.reload() }, 300);

            } else if (sessionStorage.firstOpen === "yes") {
                console.log("sessionStorage yes")
            }
        } else {// 本地存储不可用时 用cookie
            console.log("Cookie Ok")
            //获取cookie中name的值
            function getCookie(name) {
                var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
                if (arr = document.cookie.match(reg)) {
                    return unescape(arr[2]);
                }
                else {
                    return null;
                }
            }

            let firstOpen = getCookie("firstOpen");

            if (firstOpen !== "yes") {
                document.cookie = "firstOpen=yes";
                console.log("refresh")
                setTimeout(function () { location.reload() }, 600)
            } else if (firstOpen === "yes") {
                console.log("cookie yes");
            }
        }

    },

    //跑马灯
    marquee: function () {
        var slide = document.getElementById("slide");
        var slide1 = document.getElementById("slide1");
        var slide2 = document.getElementById("slide2");
        slide2.innerHTML = document.getElementById("slide1").innerHTML;

        function Marquee() {
            if (slide.scrollLeft - slide2.offsetWidth >= 0) {
                slide.scrollLeft -= slide1.offsetWidth;
            }
            else {
                slide.scrollLeft++;
            }
        }
        var myvar = setInterval(Marquee, 35);
        slide.onmouseout = function () { myvar = setInterval(Marquee, 35); }
        slide.onmouseover = function () { clearInterval(myvar); }
    },
}
winPage.init();